package org.bugjail.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bugjail.data.Person;
import org.bugjail.dataaccess.jdbc.objects.JDBCDAOFactory;
import org.bugjail.dataaccess.objects.PersonDAO;

/**
 * Servlet implementation class for Servlet: LoginServlet
 *
 */
 public class LoginServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PersonDAO dao = JDBCDAOFactory.getDAOFactory().getPersonDAO();
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		Person p = dao.retrieveByLoginAndPassword(login, password);
		if (p != null) {
			System.out.println("URL: "+request.getRequestURL().toString()+"?"+request.getQueryString());
			
			request.getSession().setAttribute("user", p);
			response.sendRedirect("HomeServlet");
		}
	}   	  	    
}